<template>
  <div class="app-container">
    <el-radio-group v-model="queryParams.sourceType" @change="resetQuery" style="margin-bottom: 30px;">
      <el-radio-button :label="item.value" v-for="(item,index) in dict.type.task_source" :key="index">{{item.label}}</el-radio-button>
    </el-radio-group>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入任务标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <TableCard>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5" v-if="queryParams.sourceType == 1">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['cooperate:mission:add']"
          >新增
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

        <el-table :border="Global.tableShowBorder" v-loading="loading" :data="list">
          <el-table-column label="任务标题" align="center" prop="title"/>
          <el-table-column label="任务内容" align="center" prop="content">
            <template slot-scope="{row}">
              <span v-html="row.content"></span>
            </template>
          </el-table-column>
          <el-table-column label="是否已查看" align="center" prop="viewed">
            <template slot-scope="{row}">
              <dict-tag :options="dict.type.comm_yes_no" :value="row.viewed"/>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime"></el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200" v-if="queryParams.sourceType == 1">
            <template slot-scope="{row}">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(row)"
                v-hasPermi="['cooperate:mission:update']"
              >修改
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(row)"
                v-hasPermi="['cooperate:mission:delete']"
              >删除
              </el-button>
              <el-button
                size="mini"
                type="text"
                @click="handleZHuanfa(row)"
                v-hasPermi="['cooperate:mission:forward']"
              >转发
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
    </TableCard>
    <!-- 添加或修改我的任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" :width="title == '转发' ? '90%' : '1000px'" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <div v-if="title == '转发'">
          <jigouUserCommpon v-model="form.userIds"></jigouUserCommpon>
        </div>
        <el-row v-else>
          <el-col :span="24">
            <el-form-item label="任务标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入任务标题"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经度" prop="lon">
              <el-input v-model="form.lon" placeholder="请输入经度"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="纬度" prop="lat">
              <div class="flex_hcneter">
                <el-input class="flex_flex" v-model="form.lat" placeholder="请输入纬度"/>
                <el-button type="primary" style="margin-left:15px;" @click="amapSelect">获取位置</el-button>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入地址" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="图片" prop="images">
              <imageUpload @fileSuccess="fileSuccess"
                :fileType="['bmp','jpg','png','gif',]" returnData="Array" fileName="files" :fileData="{'bizType':'mission',}"
                action="/oa-pcapi/cooperate/file/uploadAtt"
                v-model="form.images" :limit="100"></imageUpload>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="附件" prop="attrs"><el-button type="primary" icon="el-icon-plus" @click="addList('attrs')"></el-button></el-form-item>
          </el-col>
          <el-row v-for="(item,index) in form.attrs" :key="'attrs' + index">
            <el-col :span="24">
              <el-form-item label="文件名称">
                <el-input v-model="item.fileName"  placeholder="请输入文件名称"/>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="文件路径">
                <div class="flex_hcneter">
                  <FileUpload :limit="1"  v-model="item.url"
                  :fileType="null" returnData="Array"
                  fileName="files" :fileData="{'bizType':'mission',}"
                  action="/oa-pcapi/cooperate/file/uploadAtt" btnName="选择"></FileUpload>
                  <el-button style="margin-left:10px;" v-if="form.attrs.length > 1"
                  @click="delList(index,'attrs')" type="primary" icon="el-icon-minus"></el-button>
                </div>
              </el-form-item>
            </el-col>
          </el-row>
          <el-col :span="24">
            <el-form-item label="任务内容" prop="content">
              <el-input v-model="form.content" type="textarea" :rows="6" placeholder="请输入任务内容"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 地图弹窗 -->
    <mapLog ref="mapLogs" @mapSubmit="mapSubmit" :formObj="form" isDetail :formType="{
      'longitude':'lon',
      'latitude':'lat',
      'details':'address',
    }"></mapLog>
  </div>
</template>

<script>
import {listMission, getMission, addMission, updateMission, delMission,getUserDeptTree,forwardMission,} from "@/api/collaWorryFree/myJob";
import jigouUserCommpon from "@/components/jigouUserCommpon";
import mapLog from "@/components/mapLog/index";
export default {
  name: "MyJob",
  dicts: ['task_source','comm_yes_no','dept_type',],
  components: { jigouUserCommpon,mapLog, },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sourceType: 1,title:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [ {required: true, message: "任务标题不能为空", trigger: "blur"} ],
        content: [ {required: true, message: "任务内容不能为空", trigger: "blur"} ],
        lon: [ {required: true, message: "经度不能为空", trigger: "blur"} ],
        lat: [ {required: true, message: "纬度不能为空", trigger: "blur"} ],
      },
      bumen:[],//部门数据
      flag:false,
    };
  },
  created() {
      this.getList();
  },
  methods: {
    addList(ziduan){//添加列表
      this.form[ziduan].push({})
    },
    delList(index,ziduan){//删除列表
      this.form[ziduan].splice(index,1)
    },
    amapSelect(){//地图打开
      this.$refs.mapLogs.mapLoad(this.form)
    },
    //地图选择保存
    mapSubmit(form){
      this.form = {...form}
    },
    fileSuccess(e){//图片上传成功
      console.log('fileSuccess',e)
      let imgs = e || []
      this.form.imgs = imgs.map(item=>{
        return{...item,fileName:item.name,}
      })
    },
    handleZHuanfa(e){//转发
      this.form.id = e.id
      this.open = true;
      this.title = "转发";
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listMission(this.queryParams).then(response => {
        let rows = response.rows || [];
        this.list = rows.map(item=>{
          return {...item,content:this.$changeLine(item.content || ''),}
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        imgs:[],attrs:[],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加我的任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMission(id).then(response => {
        let data = response.data || {};
        let imgs = data.imgs || []
        let imgss = imgs.map(item=>item.url).toString()
        this.form = {
          ...data,
          images:imgss,
          imgs:imgs,
        }
        this.open = true;
        this.title = "修改我的任务";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.title == '转发') {
            let form = {...this.form}
            let userIds = form.userIds || []
            if(!userIds.length) return this.$modal.msgError("转发用户不能为空");
            forwardMission(form).then(response => {
              this.$modal.msgSuccess("转发成功");
              this.open = false;
              this.getList();
            });
          }else{
            if (this.form.id != undefined) {
              updateMission(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addMission(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids
      this.$modal.confirm('是否确认删除该数据项？').then(function () {
          return delMission(id);
      }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    }
  }
};
</script>
